/*
 * Copyright 2009 Thor Hovden
 *  
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *  
 *     http://www.apache.org/licenses/LICENSE-2.0
 *  
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
 * either express or implied. See the License for the specific
 * language governing permissions and limitations under the
 * License. 
 */
package eviva.application;

import java.awt.MenuBar;
import java.awt.Toolkit;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import dev.sketch.Action;
import dev.sketch.Face;
import eviva.exceptions.Showstopper;
import eviva.structure.MissionControl;

/**
 * Represents an Eviva application. As such, it should basically start to examine a specified conceptual model and
 * initiate appropriate handling.
 * <p>
 * On a later stage, in order to improve performance, some handling will be given separate threads so that the
 * application will run multithreaded. This will require us to avoid deadlocks and other difficult parallel
 * programming issues.
 * 
 * @author Thor Hovden
 */
public class Application {
	private MissionControl mission = new MissionControl();
	private Appfig appfig;

	private int displays = 0; // number of instantiated displays

	/**
	 * Initiate application object, detect model
	 * 
	 * @param name application name
	 * @param source (only filename is supported so far)
	 * @throws Showstopper
	 */
	public Application(String name, String source) throws Showstopper {
		super();
		MissionControl.Mission m2 = mission.set(2, "Initialize " + name + " application");

		appfig = Appfig.parseFile(mission, source);

		mission.complete(m2);
	}

	/**
	 * Start to execute this application.
	 * <p>
	 * Performance can later be improved by threading intense activities.
	 * 
	 * @throws Showstopper
	 */
	public void start() {
		MissionControl.Mission m2 = mission.set(2, "Start " + appfig.getAppname() + " application");

		MenuBar mb = new MenuBar();

		for (Action a : appfig.getActions()) {
			switch (a.getType()) {
			case DISPLAY:
				Face mainFace = new Face(mission, appfig.getAppname(), a);
				mainFace.addWindowListener(new WindowAdapter() {
					public void windowClosing(WindowEvent evt) {
						end();
					}
				});
				mainFace.setIconImage(Toolkit.getDefaultToolkit().getImage("etc/eviva-icon.gif"));
				mainFace.setMenuBar(mb);
				displays++;
				break;
			// TODO handle more application actions
			}
		}

		mission.complete(m2);
	}

	/**
	 * Is called whenever a display is closed.
	 */
	private void end() {
		if (displays > 0) {
			displays--;
		}
		if (displays == 0) {
			// TODO flush outdata, close connections, clean up before application exits
			System.exit(0);
		}
	}

	public String getMissionLog() {
		return mission.getMissionLog();
	}
}
